﻿<?php 
	class data
{
	function con()
	{
		$con=mysql_connect("localhost","root","");
			if(!$con)
			{
				die('mất kết nối !');
			}
			mysql_select_db("db_dcr");
			mysql_query("SET NAMES UTF8");
			return $con;
	}
	
	function base_url($url)
	{
		$base_url="http://localhost:81/";
		$kq=$base_url.$url;
		return $kq;
	}
	
	function dis()
	{
		mysql_close($this->con());
	}
	
	function idu_no_msg_spam($sql)//sử dung dể update 
	{
		$link=$this->con();
		$kq=mysql_query($sql,$link);
		$this->dis();	
	}
	
	function idu_no_msg($sql)
	{
		if($this->spam(5)!=true)
		{
			$link=$this->con();
			$kq=mysql_query($sql,$link);
		}
		else
		{
			//
		}
		$this->dis();	
	}
	
	function idu($sql)
	{
		if($this->spam(5)!=true)
		{
			$link=$this->con();
			if(!$kq=mysql_query($sql,$link))
			{
				echo 'Lổi truy vấn - dữ liệu đã tồn tại';
			}
			else
			{
				echo 'Thành công';
			}
		}
		else
		{
			//
		}
				$this->dis();
	}
	//hàm dếm
	//query
	//SELECT * FROM tên_bảng WHERE điều kiện
	function sum_rows($sql)
	{
		$link=$this->con();
		$kq=mysql_query($sql,$link);
		$r=mysql_num_rows($kq);
		$this->dis();
		return $r;
	}
	
	function slt_option_ctr($id)
	{
		$link=$this->con();
		$sql="select * from tb_ctr where cap='$id' ORDER BY ten ASC";
		$kq=mysql_query($sql,$link);
		echo "<option></option>";
		while($rows=mysql_fetch_array($kq))
		{
			$id=$rows['id'];
			$ten=$rows['ten'];
			$cap=$rows['cap'];
			echo "<option value='$id,$ten'>$ten</option>";
		}
		$this->dis();
	}
	// hàm kiem tra email
	//lấy giá trị có trong 1 cột khi có giá trị 
	function query($sql,$cot)
	{
		$link=$this->con();
		$kq=mysql_query($sql,$link);
		$rows=mysql_fetch_array($kq);
		return $rows[$cot];
		$this->dis();
	}
	
	// hàm kiem tra email
	function check_email($email) 
	{  
            if (strlen($email) == 0) return false;
            if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) return true;
            return false;
    }// hàm ki?m tra email
	//kiểm tra spam robot
	
	function spam($sec)//giây 
	{
			//$date[0]; seconds
		$date= getdate();
		if(!isset($_SESSION["spam"]))
		{
			//không tồn tại lần đầu thực hiện requet
			$_SESSION["spam"]=$date[0];
			return false;
		}
		else
		{
			$time_requet=$date[0]-$_SESSION["spam"];
			if($time_requet<=$sec)
			{
				//thời gian requet nhỏ hơn cho phép "phát hiện spam"
				return true;
			}
			else
			{
				// lớn hơn cho phép đã thực hiện requet nhưng không spam
				$_SESSION["spam"]=$date[0];
				return false;
			}
		}
	}//end
	//xét coi có phải là chủ shop hay không
	function chu_shop($domain)
	{
					 //lấy biến pass email từ cookie va session 
		if(!isset($_COOKIE["user_email"])||!isset($_COOKIE["user_pass"]))
		{
			//khong co cookie
			if(!isset($_SESSION["user_email"])||!isset($_SESSION["user_pass"]))
			{}
			else
			{
				//có seesion
				$email=$_SESSION["user_email"];
				$pass=$_SESSION["user_pass"];
			}
		}
		else
		{
			
			//co cookie
				$email=$_COOKIE["user_email"];
				$pass=$_COOKIE["user_pass"];
		}
			//lay id
			//$id=$this->query("SELECT * FROM tb_user WHERE email='$email' AND user_pass='$pass' ","id_user");
	}//end
	//get id user 
	function get_id_user_upload()
	{
					 //lấy biến pass email từ cookie va session 
		if(!isset($_COOKIE["user_email"])||!isset($_COOKIE["user_pass"]))
		{
			//khong co cookie
			if(!isset($_SESSION["user_email"])||!isset($_SESSION["user_pass"]))
			{}
			else
			{
				//có seesion
				$email=$_SESSION["user_email"];
				$pass=$_SESSION["user_pass"];
			}
		}
		else
		{
			
			//co cookie
				$email=$_COOKIE["user_email"];
				$pass=$_COOKIE["user_pass"];
		}
			//lay id
				$id=$this->query("SELECT * FROM tb_user WHERE email='$email' AND user_pass='$pass' ","id_user");
				//lay email
		return $id;
	}//end uload
	
	function get_id_user()
	{
					 //lấy biến pass email từ cookie va session 
		if(!isset($_COOKIE["user_email"])||!isset($_COOKIE["user_pass"]))
		{
			//khong co cookie
			if(!isset($_SESSION["user_email"])||!isset($_SESSION["user_pass"]))
			{
				header('Location:'.$this->base_url("home/dangky").'');
				//khong co session	
			}
			else
			{
				//có seesion
				$email=$_SESSION["user_email"];
				$pass=$_SESSION["user_pass"];
			}
		}
		else
		{
			
			//co cookie
				$email=$_COOKIE["user_email"];
				$pass=$_COOKIE["user_pass"];
		}
			//lay id
				$id=$this->query("SELECT * FROM tb_user WHERE email='$email' AND user_pass='$pass' ","id_user");
				//lay email
		return $id;
	}//end get id user
	//kiểm tra dang nhap
	function kt_dn_tf_ajax()
	{
		//lấy biến pass email từ cookie va session 
		if(!isset($_COOKIE["user_email"])||!isset($_COOKIE["user_pass"]))
		{
			//khong co cookie
			if(!isset($_SESSION["user_email"])||!isset($_SESSION["user_pass"]))
			{	
				//khong co session	
			}
			else
			{
				//có seesion
				$email=$_SESSION["user_email"];
				$pass=$_SESSION["user_pass"];
			}
		}
		else
		{	
			//co cookie
				$email=$_COOKIE["user_email"];
				$pass=$_COOKIE["user_pass"];
		}
			//lay id
				$id=$this->query("SELECT * FROM tb_user WHERE email='$email' AND user_pass='$pass' ","id_user");
				//lay email
				$get_mail=$this->query("SELECT * FROM tb_user WHERE id_user='$id' ","email");
				//lay pass
				$get_pass=$this->query("SELECT * FROM tb_user WHERE id_user='$id' ","user_pass");
		
		if($email!=$get_mail || $pass!=$get_pass)
		{return false;}else{return true;}
	}
	function kt_dn()
	{
					 //lấy biến pass email từ cookie va session 
		if(!isset($_COOKIE["user_email"])||!isset($_COOKIE["user_pass"]))
		{
			//khong co cookie
			if(!isset($_SESSION["user_email"])||!isset($_SESSION["user_pass"]))
			{
				header('Location:'.$this->base_url("home/dangky").'');
				//khong co session	
			}
			else
			{
				
				//có seesion
				$email=$_SESSION["user_email"];
				$pass=$_SESSION["user_pass"];
			}
		}
		else
		{
			
			//co cookie
				$email=$_COOKIE["user_email"];
				$pass=$_COOKIE["user_pass"];
		}
			//lay id
				$id=$this->query("SELECT * FROM tb_user WHERE email='$email' AND user_pass='$pass' ","id_user");
				//lay email
				$get_mail=$this->query("SELECT * FROM tb_user WHERE id_user='$id' ","email");
				//lay pass
				$get_pass=$this->query("SELECT * FROM tb_user WHERE id_user='$id' ","user_pass");
		
		if($email!=$get_mail || $pass!=$get_pass)
		{
			header('Location:'.$this->base_url("home/dangky").'');
		}
	}
	
	function getmailcs()
	{
		if(isset($_COOKIE["user_email"]))
		{
			$email=$_COOKIE["user_email"];	
		}
		else
		{
			$email=$_SESSION["user_email"];
		}
		return $email;
	}
	
	//kiểm tra dang nhập
	function domxml_user()
	{
		//conet select data user
		$link=$this->con();	
		$sql="select * from  tb_user";
		$kq=mysql_query($sql,$link);
		
		//open xml
		$dom=new DomDocument("1.0","utf-8"); 
			//<user>
			$users = $dom->createElement("users"); // tạo ra một phần tử tên là users
			$dom->appendChild($users); //không đóng phẩn tủ link mà típ tục tạo con cua users
															
		//row data user
		while($rows=mysql_fetch_array($kq))
		{
				/*
				$rows['id_user'];
				$rows['user_name'];
				$rows['ten'];
				$rows['dt'];
				$rows['shop_name'];
				$rows['domain'];
				$rows['keycode'];
				$rows['kh'];
				$rows['email'];
				$rows['ip_map'];
				$rows['url_img_user'];
				$rows['url_img_shop'];
				$rows['gt'];
				$rows['url_face'];
				$rows['url_gg'];
				$rows['url_tw'];
				$rows['diachi'];
				$rows['datetime_dk']; // ngày đăng ký
				$rows['datetime_gh'] // ngày gia hạng
				*/
					//<user_id>
							$user_id=$dom->createElement("user_id"); //tạo node tên 
							$user_id_text=$dom->createTextNode($rows['id_user']); //tạo text cho node 
							$user_id->appendChild($user_id_text); //add text vào node 
							$users->appendChild($user_id); //đóng node 
					//<user_id>
					//<ten>
							$ten=$dom->createElement("ten"); //tạo node tên 
							$ten_text=$dom->createTextNode($rows['ten']); //tạo text cho node 
							$ten->appendChild($ten_text); //add text vào node 
							$users->appendChild($ten); //đóng node 
					//<ten>
					//<dt>
							$dt=$dom->createElement("dt"); //tạo node tên 
							$dt_text=$dom->createTextNode($rows['dt']); //tạo text cho node 
							$dt->appendChild($dt_text); //add text vào node 
							$users->appendChild($dt); //đóng node 
					//<dt>
					//<shop_name>
							$shop_name=$dom->createElement("shop_name"); //tạo node tên 
							$shop_name_text=$dom->createTextNode($rows['shop_name']); //tạo text cho node 
							$shop_name->appendChild($shop_name_text); //add text vào node 
							$users->appendChild($shop_name); //đóng node 
					//<shop_name>
					//<domain>
							$domain=$dom->createElement("domain"); //tạo node tên 
							$domain_text=$dom->createTextNode($rows['domain']); //tạo text cho node 
							$domain->appendChild($domain_text); //add text vào node 
							$users->appendChild($domain); //đóng node 
					//<domain>
					//<email>
							$email=$dom->createElement("email"); //tạo node tên 
							$email_text=$dom->createTextNode($rows['email']); //tạo text cho node 
							$email->appendChild($email_text); //add text vào node 
							$users->appendChild($email); //đóng node 
					//<email>
					//<ip_map>
							$ip_map=$dom->createElement("ip_map"); //tạo node tên 
							$ip_map_text=$dom->createTextNode($rows['ip_map']); //tạo text cho node 
							$ip_map->appendChild($ip_map_text); //add text vào node 
							$users->appendChild($ip_map); //đóng node 
					//<ip_map>
					//<url_img_user>
							$url_img_user=$dom->createElement("url_img_user"); //tạo node tên 
							$url_img_user_text=$dom->createTextNode($rows['ip_map']); //tạo text cho node 
							$url_img_user->appendChild($url_img_user_text); //add text vào node 
							$users->appendChild($url_img_user); //đóng node 
					//<url_img_user>
					//<gt>
							$gt=$dom->createElement("gt"); //tạo node tên 
							$gt_text=$dom->createTextNode($rows['gt']); //tạo text cho node 
							$gt->appendChild($gt_text); //add text vào node 
							$users->appendChild($gt); //đóng node 
					//<gt>
					//<url_face>
							$url_face=$dom->createElement("url_face"); //tạo node tên 
							$url_face_text=$dom->createTextNode($rows['url_face']); //tạo text cho node 
							$url_face->appendChild($url_face_text); //add text vào node 
							$users->appendChild($url_face); //đóng node 
					//<url_face>
					//<url_gg>
							$url_gg=$dom->createElement("url_gg"); //tạo node tên 
							$url_gg_text=$dom->createTextNode($rows['url_gg']); //tạo text cho node 
							$url_gg->appendChild($url_gg_text); //add text vào node 
							$users->appendChild($url_gg); //đóng node 
					//<url_gg>
					//<url_gg>
							$url_tw=$dom->createElement("url_tw"); //tạo node tên 
							$url_tw_text=$dom->createTextNode($rows['url_tw']); //tạo text cho node 
							$url_tw->appendChild($url_tw_text); //add text vào node 
							$users->appendChild($url_tw); //đóng node 
					//<url_gg>
					//<diachi>
							$diachi=$dom->createElement("diachi"); //tạo node tên 
							$diachi_text=$dom->createTextNode($rows['diachi']); //tạo text cho node 
							$diachi->appendChild($diachi_text); //add text vào node 
							$users->appendChild($diachi); //đóng node 
					//<diachi>
					//<datetime_dk>
							$datetime_dk=$dom->createElement("datetime_dk"); //tạo node tên 
							$datetime_dk_text=$dom->createTextNode($rows['datetime_dk']); //tạo text cho node 
							$datetime_dk->appendChild($datetime_dk_text); //add text vào node 
							$users->appendChild($datetime_dk); //đóng node 
					//<datetime_dk>
					//<datetime_gh>
							$datetime_gh=$dom->createElement("datetime_gh"); //tạo node tên 
							$datetime_gh_text=$dom->createTextNode($rows['datetime_gh']); //tạo text cho node 
							$datetime_gh->appendChild($datetime_gh_text); //add text vào node 
							$users->appendChild($datetime_gh); //đóng node 
					//<datetime_gh>
					//<datetime_gh>
							$soluong=$dom->createElement("soluong"); //tạo node tên 
							$soluong_text=$dom->createTextNode($rows['soluong']); //tạo text cho node 
							$soluong->appendChild($soluong_text); //add text vào node 
							$users->appendChild($soluong); //đóng node 
					//<datetime_gh>
					
						   
		}		
			//<users>
		//save xml
		$dom->formatOutput=true; 
		$dom->saveXML(); 
		$dom->save('xml/user.xml');  
		
		$this->dis();
	}
	
	function utf8_urldecode($str) 
	{
    $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
    return html_entity_decode($str,null,'UTF-8');;
 	}//utf8_urldecode
	
	//up img
	function up_img($name,$size_kb,$url,$new_name)
	{
	$allowedExts = array("gif", "jpeg", "jpg", "png");
	$temp = explode(".", $_FILES[$name]["name"]);
	$extension = end($temp);
	if ((($_FILES[$name]["type"] == "image/gif")
	|| ($_FILES[$name]["type"] == "image/jpeg")
	|| ($_FILES[$name]["type"] == "image/jpg")
	|| ($_FILES[$name]["type"] == "image/pjpeg")
	|| ($_FILES[$name]["type"] == "image/x-png")
	|| ($_FILES[$name]["type"] == "image/png"))
	&& ($_FILES[$name]["size"] < $size_kb*3024)
	&& in_array($extension, $allowedExts))
	  {
	  if ($_FILES[$name]["error"] > 0)
		{}
	  else
		{if (file_exists($url. $_FILES[$name]["name"]))
		  {}
		else
		  {		  
		  move_uploaded_file($_FILES[$name]["tmp_name"],$url.$new_name);
		  return true;      
		  }
		}
	  }
	}//up img
	
	//Thumbs img theo forder
	//link chua img gioc , link chuyen thanh dang thumbs , chieu rong anh thumb
	function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
	{
	  // open the directory
	  $dir = opendir( $pathToImages );
	
	  // loop through it, looking for any/all JPG files:
	  while (false !== ($fname = readdir( $dir ))) {
		// parse path for the extension
		$info = pathinfo($pathToImages . $fname);
		// continue only if this is a JPEG image
		if ( strtolower($info['extension']) == 'jpg' ) 
		{
		 // echo "Creating thumbnail for {$fname} <br />";
	
		  // load image and get image size
		  $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
		  $width = imagesx( $img );
		  $height = imagesy( $img );
	
		  // calculate thumbnail size
		  $new_width = $thumbWidth;
		  $new_height = floor( $height * ( $thumbWidth / $width ) );
	
		  // create a new temporary image
		  $tmp_img = imagecreatetruecolor( $new_width, $new_height );
	
		  // copy and resize old image into new image 
		  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
	
		  // save thumbnail into a file
		  imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
		}
	  }
	  // close the directory
	  closedir( $dir );
	}
	//end thum img theo forder
	
	//thum theo img 
	function createThumbsImg($link_img,$thumbWidth)
	{
		 // load image and get image size
		  $img = imagecreatefromjpeg($link_img);
		  $width = imagesx( $img );
		  $height = imagesy( $img );
	
		  // calculate thumbnail size
		  $new_width = $thumbWidth;
		  $new_height = floor( $height * ( $thumbWidth / $width ) );
	
		  // create a new temporary image
		  $tmp_img = imagecreatetruecolor( $new_width, $new_height );
	
		  // copy and resize old image into new image 
		  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
	
		  // save thumbnail into a file
		  imagejpeg( $tmp_img,$link_img );
			
	}
	function createThumbsImgWH($link_img,$w,$h)
	{
		 // load image and get image size		 
		  $width = $w;
		  $height = $h;
	
		  // calculate thumbnail size
		  $new_width = $thumbWidth;
		  $new_height = floor( $height * ( $thumbWidth / $width ) );
	
		  // create a new temporary image
		  $tmp_img = imagecreatetruecolor( $new_width, $new_height );
	
		  // copy and resize old image into new image 
		  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
	
		  // save thumbnail into a file
		  imagejpeg( $tmp_img,$link_img );
			
	}
	
	//end thum img 
	//convet ingto jpg
	function convertImage($originalImage, $outputImage, $quality)
	{
		// jpg, png, gif ?
		$exploded = explode('.',$originalImage);
		$ext = $exploded[count($exploded) - 1]; 
	
		if (preg_match('/jpg|jpeg/i',$ext))
			$imageTmp=imagecreatefromjpeg($originalImage);
		else if (preg_match('/png/i',$ext))
			$imageTmp=imagecreatefrompng($originalImage);
		else if (preg_match('/gif/i',$ext))
			$imageTmp=imagecreatefromgif($originalImage);
		else
			return 0;
	
		// quality is a value from 0 (worst) to 100 (best)
		imagejpeg($imageTmp, $outputImage, $quality);
		imagedestroy($imageTmp);
	
		return 1;
	}
	//end convet
	//format_number doi tien ve int
	function format_number($num)
	{
		$kq=str_replace(',','',$num);
		$kq=(int)$kq;
		return $kq;
	}
}
?>